package com.ruoyi.common.utils;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.entity.WxUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 安全服务工具类
 *
 * @author ruoyi
 */
public class SecurityUtils {
    /**
     * 用户ID
     **/
    public static String getUserId() {
        try {
            return getLoginUser().getUserId();
        } catch (Exception e) {
            throw new ServiceException("获取用户ID异常" , HttpStatus.UNAUTHORIZED);
        }
    }

    public static String getNickName() {
        try {
            return getLoginUser().getUser().getNickName();
        } catch (Exception e) {
            throw new ServiceException("获取用户ID异常", HttpStatus.UNAUTHORIZED);
        }
    }

    /**
     * 获取部门ID
     **/
    public static String getDeptId() {
        try {
            return getLoginUser().getDeptId();
        } catch (Exception e) {
            throw new ServiceException("获取部门ID异常" , HttpStatus.UNAUTHORIZED);
        }
    }

    /**
     * 获取用户账户
     **/
    public static String getUsername() {
        try {
            String userName = "";
            LoginUser loginUser = getLoginUser();
            if (loginUser != null) {
                userName = getLoginUser().getUsername();
                if (StringUtils.isBlank(userName) && "wxUser".equals(loginUser.getUserType())) {
                    userName = getLoginUser().getWxUser().getNickName();
                }
            } else {
                throw new ServiceException("获取用户账户异常" , HttpStatus.UNAUTHORIZED);
            }
            return userName;
        } catch (Exception e) {
            throw new ServiceException("获取用户账户异常" , HttpStatus.UNAUTHORIZED);
        }
    }

    /**
     * 获取wxUserId
     **/
    public static String getWxUserId() {
        try {
            LoginUser loginUser = getLoginUser();
            return loginUser.getWxUserId();
        } catch (Exception e) {
            throw new ServiceException("获取用户账户异常" , HttpStatus.UNAUTHORIZED);
        }
    }

    /**
     * 获取wxUserId
     **/
    public static WxUser getWxUser() {
        try {
            LoginUser loginUser = getLoginUser();
            return loginUser.getWxUser();
        } catch (Exception e) {
            throw new ServiceException("获取用户账户异常" , HttpStatus.UNAUTHORIZED);
        }
    }

    /**
     * 获取用户
     **/
    public static LoginUser getLoginUser() {
        try {
            return (LoginUser) getAuthentication().getPrincipal();
        } catch (Exception e) {
            throw new ServiceException("获取用户信息异常" , HttpStatus.UNAUTHORIZED);
        }
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 生成BCryptPasswordEncoder密码
     *
     * @param password 密码
     * @return 加密字符串
     */
    public static String encryptPassword(String password) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(password);
    }

    /**
     * 判断密码是否相同
     *
     * @param rawPassword     真实密码
     * @param encodedPassword 加密后字符
     * @return 结果
     */
    public static boolean matchesPassword(String rawPassword, String encodedPassword) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }

    /**
     * 是否为管理员
     *
     * @param userId 用户ID
     * @return 结果
     */
    public static boolean isAdmin(String userId) {
        return "1".equals(userId);
    }


    public static void main(String[] args) {
//        System.out.println(SecurityUtils.encryptPassword("370873104200"));
        String[] arr = "18905370188@abc,18325473912@abc,17806379990@abc,15910010700@abc,13385376669@abc,18053767821@abc,13953768223@abc,15069726285@abc,15553737899@abc,05376888989@abc,05372228288@abc,05376983999@abc,05376885766@abc,15953475733@abc,13954252934@abc,18054552767@abc,13365471000@abc,055162220088@abc,05378888888@abc,17753733267@abc,05376836661@abc,15305370533@abc,13355112868@abc,05376888888@abc,18605476829@abc,13305471036@abc,13563794387@abc,13287292225@abc,18266866660@abc,15265785368@abc,13705470917@abc,3212257@abc,05376889997@abc,3777784@abc,13455272849@abc,13563777934@abc,05376881131@abc,05376886788@abc,17661306238@abc,05372982008@abc,15588746666@abc,05376662222@abc,13054966022@abc,05376975566@abc,05376889777@abc,05376881988@abc,05372246999@abc,15634449780@abc,15315476526@abc,05376888568@abc,15963751949@abc,18053779135@abc,15969977958@abc,19953733311@abc,15092688158@abc,05376864898@abc,15092670664@abc,18678748653@abc,18266812888@abc,18353788219@abc,13111111111@abc,15863722925@abc,18653793233@abc,05372605138@abc,13053758658@abc,13287222687@abc,18463732124@abc,13639417228@abc,15066242928@abc,15318419627@abc,18653793368@abc,18805479955@abc,4001618798@abc,13122222222@abc,18853119858@abc,13563736150@abc,18678761218@abc,05372166870@abc,05376866566@abc,15053709933@abc,05377171868@abc,13105474444@abc,13676379283@abc,13371206018@abc,13355182367@abc,19905477036@abc,13345186516@abc,13954776389@abc,05373177818@abc,17562050105@abc,05372275959@abc,13793750586@abc,13341217360@abc,15265785368@abc,13853764026@abc,17895371777@abc,15864557771@abc,05377080808@abc,18863787616@abc,15265785368@abc,13355181099@abc,13020695136@abc,4000537307@abc,15864550376@abc,15053767525@abc,05372579155@abc,15863721777@abc,05372525265@abc,19811897333@abc,18063231203@abc,18005377073@abc,05372275299@abc,13791742075@abc,05372210803@abc,05376825989@abc,05376986669@abc,05376880887@abc,6988136@abc,3713668@abc,05373379617@abc,18653788223@abc,15092667792@abc,18854703115@abc,05372175678@abc,05372750057@abc,18253797627@abc,05376886188@abc,13395479399@abc,15054863119@abc,05372287266@abc,13355163188@abc,05376970366@abc,13371232533@abc,05372384567@abc,15253789266@abc,05373519255@abc,05376805555@abc,05376988881@abc,17753781777@abc,13721915716@abc,13054990111@abc,4000660537@abc,05376813666@abc,17661330698@abc,05371526571@abc,05376888688@abc,05376882555@abc,05372226660@abc,05376988587@abc,6988799@abc,05376988105@abc,15265780888@abc,13953752596@abc,05373232300@abc,05376824766@abc,15588771383@abc,15269753095@abc,05376886969@abc,13791727548@abc,13153771106@abc,05376976088@abc,05372659566@abc,18653721000@abc,13953771316@abc,15253736036@abc,13623512868@abc,13853707182@abc,15965373757@abc,05376836111@abc,15995532064@abc,15662948228@abc,13563752932@abc,18954064666@abc,05376857588@abc,05376988399@abc,05372550708@abc,05376977588@abc,05372579696@abc,15505378266@abc,05376988486@abc,05373219177@abc,05332263922@abc,059185682728@abc,13792325857@abc,05372884466@abc,05376807678@abc,05372239666@abc,05376974969@abc,15853746966@abc,15853749966@abc,05376980577@abc,05376982088@abc,4006920001@abc,05372897567@abc,0103163016@abc,13954751359@abc,13287208080@abc,18753737222@abc,4000006667@abc,05372200033@abc,05372378500@abc,15653713633@abc,13953769199@abc,13305370831@abc,05372273709@abc,18653792069@abc,13805377168@abc,13792373568@abc,05373379600@abc,05376827688@abc,053769866185@abc,3462307@abc,05376858848@abc,05376178799@abc,15275766666@abc,05376985199@abc,18254792111@abc,13854710736@abc,05373218785@abc,05376983151@abc,05376885678@abc,18371504444@abc,05372205841@abc,05376666486@abc,13563721953@abc,05376982500@abc,05376980766@abc,6988968@abc,15106727785@abc,05372574888@abc,15054877369@abc,13054961569@abc,15265732181@abc,05376988111@abc,02981151631@abc,05376696867@abc,19157069696@abc,13983031384@abc,17661306238@abc,13181334520@abc,15054795587@abc,54775089@abc,15154721881@abc,13258005086@abc,15053720999@abc,15969792789@abc,18253748123@abc,13562412022@abc,19157109264@abc,18564296540@abc,18615272111@abc,15054795587@abc,13144444444@abc,13792330909@abc,13792346757@abc,18660757848@abc,13054985980@abc,17661305278@abc,13133333333@abc,17649866856@abc,13910095287@abc,05376050823@abc,16216156777@abc,13173197127@abc,17116565376@abc,18369809111@abc,05376662078@abc,16688018287@abc,1336537@abc,18553766910@abc,13287208080@abc,13405372253@abc,17753733267@abc,15063747652@abc,13012620719@abc,17562035800@abc,16688018287@abc,13953735655@abc,05376889999@abc,15305473777@abc,15305473777@abc,05372915097@abc,15063769720@abc,15106762705@abc,05376883799@abc,05376451166@abc,05376888558@abc,15953472670@abc,18905373288@abc,14717772144@abc,0537245823@abc,05376607177@abc,05372291222@abc,13295373704@abc,17662135759@abc,18854720881@abc,16688018287@abc,15624091888@abc,15206746816@abc,13305377144@abc".split(",");
        for(String pw : arr){
            System.out.println(SecurityUtils.encryptPassword(pw));
        }

    }
}
